package org.springleaf.core.mvc.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.AbstractView;
import org.springleaf.core.json.JsonUtils;

/**
 * JSON视图
 * @author leaf
 *
 */
public class JSONView extends AbstractView {

	/**
	 * 实现渲染方法
	 */
	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String json = null;
		if (model.containsKey(JSONModelAndView.KEY)) {
			json = JsonUtils.toJSON(model.get(JSONModelAndView.KEY));
		} else {
			json = JsonUtils.toJSON(model);
		}
		response.getWriter().println(json);
	}

}
